<template>
	<view>
		<view class="header">
			<view class="input-box">
				<input placeholder="默认关键字" v-model="name" placeholder-style="color:#777;" confirm-type="search"
					@confirm="doSearch" />
				<uni-icons type="search" color="#777" size="20"></uni-icons>
			</view>
		</view>
		<!-- 占位 -->
		<view class="place">
			<view class="" v-if="name != ''">
				搜索商品:{{name}}
			</view>
		</view>
		<view class="no-list" v-if="goodsList.length == 0 && is_search">暂无数据</view>
		<!-- 商品列表 -->
		<view class="goods-list">
			<view class="product-list">
				<view class="product" v-for="(goods) in goodsList" :key="goods.id" @tap="toGoods(goods)">
					<image :src="goods.cover_path"></image>
					<view class="name">{{goods.name}}</view>
					<view class="info">
						<view class="price">￥{{goods.price}}</view>
						<view class="slogan">销量:{{goods.total_sales}}</view>
					</view>
				</view>
			</view>
			<!-- <view class="loading-text" v-show="flag">{{ loadingText }}</view> -->
		</view>
	</view>
</template>

<script>
	import {
		searchGoods
	} from "@/api/api.js"
	export default {
		data() {
			return {
				goodsList: [],
				loadingText: "搜索中...",
				headerTop: "0px",
				headerPosition: "fixed",
				page: 1,
				flag: false,
				name: '',
				last_page: 1,
				is_search: false
			};
		},
		onLoad: function(option) { //option为object类型，会序列化上个页面传递的参数
			// #ifdef APP-PLUS
			this.headerTop = '0'
			// #endif
			// #ifdef H5
			this.headerTop = '44px'
			// #endif
		},
		onReachBottom() {
			if (this.page < this.last_page) {
				this.page++
				this.doSearch()
			}
		},
		methods: {
			//商品跳转
			toGoods(e) {
				uni.navigateTo({
					url: '/pages/goodsdetail/index?id=' + e.id
				});
			},
			doSearch(e) {
				var name = e.detail.value.trim()
				if (name == '') {
					return false
				}
				this.flag = true
				this.is_search = true
				this.loadingText = '搜索中...';
				this.goodsList = [];
				searchGoods({
					name: name,
					page: this.page
				}).then(res => {
					if (res.code == 0) {
						this.goodsList = this.goodsList.concat(res.data.data)
						this.flag = false
						this.last_page = res.data.last_page
					} else {
						this.flag = true
						this.loadingText = res.msg;
					}
				})
			}
		}

	}
</script>

<style lang="scss">
	page {
		background: #f7f7f7;
	}

	.icon {
		font-size: 26upx;
	}

	.input-box {
		width: 95%;
		height: 60upx;
		background-color: #fff;
		border-radius: 30upx;
		position: relative;
		display: flex;
		align-items: center;
		margin: 0 auto;

		input {
			padding-left: 28upx;
			height: 28upx;
			font-size: 28upx;
			width: 85%;
		}
	}

	.header {
		padding: 10px 0;
	}

	.place {
		width: 100%;
		display: flex;
		padding-left: 15px;
		align-items: center;
		color: #979797;
		font-size: 12px;
		background-color: #f7f7f7;
		height: 90upx;

	}

	.goods-list {
		.loading-text {
			width: 100%;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 60upx;
			color: #979797;
			font-size: 24upx;
		}

		.product-list {
			width: 92%;
			padding: 0 4% 3vw 4%;
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;

			.product {
				width: 48%;
				border-radius: 20upx;
				background-color: #fff;
				margin: 0 0 15upx 0;
				box-shadow: 0upx 5upx 25upx rgba(0, 0, 0, 0.1);

				image {
					width: 100%;
					border-radius: 20upx 20upx 0 0;
					height: 160px;
				}

				.name {
					width: 92%;
					padding: 10upx 4%;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					-webkit-line-clamp: 2;
					text-align: justify;
					overflow: hidden;
					font-size: 30upx;
					padding-bottom: 0;
				}

				.info {
					display: flex;
					justify-content: space-between;
					align-items: flex-end;
					width: 92%;
					padding: 10upx 4% 10upx 4%;

					.price {
						color: #e65339;
						font-size: 30upx;
						font-weight: 600;
					}

					.slogan {
						color: #807c87;
						font-size: 24upx;
					}
				}
			}

		}
	}
</style>